package cz.acies.uniql.sql.element;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Element;
import org.dom4j.dom.DOMElement;


import cz.acies.uniql.dialect.Dialect;
import cz.acies.uniql.sql.UniQLExpr;
import cz.acies.utils.StringUtils;

/**
 * Trida UniQLConditionBetweenParams.
 * 
 * Priklad pouziti:<br>
 * sql.add(new UniQLCondition("calendar_task.START", "BETWEEN",<br>
 * 	   new UniQLConditionBetweenParams(new Object[] {"'2011-03-01 00:00:00'", "'2011-03-31 00:00:00'"})<br>
 * ));<br>
 */
public class UniQLConditionInParams extends UniQLExpr
{
	private static final long serialVersionUID = 8995227361324689627L;
	
	/** The params. */
	private List<Object> params = new ArrayList<Object>();
	
	/**
	 * Instantiates a new uni ql condition between params.
	 */
	public UniQLConditionInParams() {}
	
	/**
	 * Instantiates a new uni ql condition between params.
	 *
	 * @param args the args
	 */
	public UniQLConditionInParams(Object[] args) {
		super();
		this.add(args);
	}

	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.UniQLExpr#translate()
	 */
	@Override
	public String translate() throws Exception {
		StringBuilder sb = new StringBuilder("");
		for (Iterator<Object> iterator = params.iterator(); iterator.hasNext();) {
			Object obj = iterator.next();
			if (obj instanceof UniQLExpr) {
				UniQLExpr expr = (UniQLExpr)obj;
				expr.setUniQLProperties(this);
				sb.append("" +expr.translate());
			}
			if (iterator.hasNext()) sb.append(", ");
		}
		return "("+sb.toString()+")";
	}
	
	/**
	 * Metoda prid� dals� parametry.
	 *
	 * @param args
	 * @return
	 */
	public UniQLConditionInParams add(Object[] args) {
		for (Object obj : args) {
			if (obj instanceof String) {
				String s = ((String)obj).trim();
				params.add(makeExpr(obj));
			} else {
				params.add(makeExpr(obj));
			}
		}
		return this;
	}

	
	/**
	 * Metoda prepise puvodni parametry novzmi.
	 *
	 * @param args
	 * @return
	 */
	public UniQLConditionInParams set(Object[] args) {
		params.clear();
		for (Object obj : args) {
			if (obj instanceof String) {
				String s = ((String)obj).trim();
				params.add(makeExpr(obj));
			} else {
				params.add(makeExpr(obj));
			}
		}
		return this;
	}

	private UniQLExpr makeExpr(Object obj) {
		if (obj instanceof String) {
			String s = ((String)obj).trim();			
			UniQLExpr expr = null;
			if (s.matches(StringUtils.NUMERIC_PATTERN) ||
				s.matches(StringUtils.INTEGER_PATTERN) ||
				s.matches("\\'.*\\'")) {
				
				expr = new UniQLValue(obj.toString());						
			} else if (s.matches("\\w*?\\.?\\w+")) {
				expr = new UniQLColumn(obj.toString());	
			} else {
				expr = new UniQLValue(obj.toString());						
			}
			expr.setDialect(getDialect());
			expr.setAliasMap(getAliasMap());
			return expr;
		} else if (obj instanceof UniQLExpr) {
			((UniQLExpr)obj).setDialect(getDialect());
			((UniQLExpr)obj).setAliasMap(getAliasMap());
			return ((UniQLExpr)obj);
		}
		return null;
	}

	// -----  -----------------------------------------------------------------
	
	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.UniQLExpr#toString()
	 */
	public String toString() {
		return "" + this.getClass().getSimpleName() + " ( " + params + " )";
	}
	
}
